export const DelCookie = (name) => {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = GetCookie(name);
  if (cval != null)
    document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};

export const GetCookie = (name) => {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg)) {
    return unescape(arr[2])
  } else {
    return null
  }
};

export const calculateWidth = () =>{
  let ths = Array.from(document.querySelectorAll('th'));
  let width= ths.map((value)=>{
    return Number(value.width.slice(0,value.width.indexOf('p')))
  }).reduce((t,n)=>t+n);
  let table =document.querySelector('table');
  table.style.width = width+'px';
};

export const SetCookie = (name, value) => {
  var Days = 30
  var exp = new Date()
  exp.setTime(exp.getTime() + 60 * 60 * 1000*6)//过期时间 60分钟*6
  document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()
};

//验证手机号，验证密码，验证码
/**
 * @author Rui.Zhang
 * @description 判断数据是否为手机号
 * @param {String} str_data 待校验的数据
 * @returns {Boolean}, true:是手机号
 **/
export const isMobile = function (str_data) {
  str_data = str_data || String(this);
  var length = str_data.length;
  return length == 11 && /^1\d{10}$/.test(str_data);
};
export const isEmail = function (str_data) {
  str_data = str_data || String(this);
  return  /^([a-zA-Z0-9_-]{1,16})@([a-zA-Z0-9]{1,9})(\.[a-zA-Z0-9]{1,9}){0,3}(\.(?:com|net|org|edu|gov|mil|cn|us)){1,4}$/.test(str_data);
};


export const myHandleStatus = (value)=>{
  let newValue;
  switch (value) {
    case 0:newValue='未完成';break;
    case 1:newValue='等待审核';break;
    case 2:newValue='初审通过';break;
    case 3:newValue='副审通过';break;
    case 4:newValue='等待面签';break;
    case 5:newValue='放款';break;
    case 6:newValue='借款失败';break;
    default:newValue='未完成';break
  }
  return newValue;
}
export const myHandlePayChannel =(value)=>{
  if(value == 1){
    return '微信'
  }else if(value==2){
    return '支付宝'
  }else {
    return value
  }
};
export const myHandleFeesStatus = (value)=>{
  if(value == 1){
    return '已支付'
  }else if(value==0){
    return '未支付'
  }else {
    return value
  }
};

export const msg = (err,toast)=>{
  try{
    if(err.response.data.errors){
      let keys = Object.keys(err.response.data.errors);
      toast(err.response.data.errors[keys[0]][0]);
    }else {
      toast(err.response.data.msg,)
    }
  }catch (e) {
    toast({
      message:'网络超时，请刷新',
      duration:1000
    })
  }
};

export const randomChar = (l) => {
  let  x="0123456789qwertyuioplkjhgfdsazxcvbnm";
  let  tmp="";
  let timestamp = new Date().getTime();
  for(let i=0;i<  l;i++)  {
    tmp  +=  x.charAt(Math.ceil(Math.random()*100000000)%x.length);
  }
  return  timestamp+tmp;
};





